package com.jhgsys.internal.accident.controller;

import com.jhgsys.internal.accident.dto.AccidentAlertsDto;
import com.jhgsys.internal.accident.entity.AccidentAlerts;
import com.jhgsys.internal.accident.service.IAccidentAlertsFileService;
import com.jhgsys.internal.accident.service.IAccidentAlertsService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 安全警报 Controller
 *
 * @author zhangwei
 * @date 2024-08-22 09:19:50
 */
@Slf4j
@Validated
@Controller
public class AccidentAlertsController extends BaseController {

    @Autowired
    private IAccidentAlertsService accidentAlertsService;

    @Autowired
    private IAccidentAlertsFileService accidentAlertsFileService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAlerts")
    @RequiresPermissions("accidentAlerts:view")
    public String accidentAlertsIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("accidentAlerts/accidentAlertsList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAlerts/add")
    @RequiresPermissions("accidentAlerts:add")
    public String accidentAlertsAdd(){
        return FebsUtil.view("accidentAlerts/accidentAlertsEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAlerts/update/{id}")
    @RequiresPermissions("accidentAlerts:update")
    public String accidentAlertsUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("accidentAlerts", accidentAlertsService.findAccidentAlertsById(id));
        return FebsUtil.view("accidentAlerts/accidentAlertsEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAlerts/detail/{id}")
    @RequiresPermissions("accidentAlerts:view")
    public String accidentAlertsDetail(Model model, @PathVariable("id") String id) {
        model.addAttribute("accidentAlerts", accidentAlertsService.findAccidentAlertsById(id));
        return FebsUtil.view("accidentAlerts/accidentAlertsDetail");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-08-22 09:19:50
     */
    @GetMapping("accidentAlerts")
    @ResponseBody
    @RequiresPermissions("accidentAlerts:view")
    public FebsResponse getAllAccidentAlertss(AccidentAlerts accidentAlerts) {
        return new FebsResponse().success().data(accidentAlertsService.findAccidentAlertsList(accidentAlerts));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-08-22 09:19:50
     */
    @GetMapping("accidentAlerts/list")
    @ResponseBody
    @RequiresPermissions("accidentAlerts:view")
    public FebsResponse accidentAlertsList(QueryRequest request, AccidentAlertsDto accidentAlertsDto) {
        Map<String, Object> dataTable = getDataTable(this.accidentAlertsService.findAccidentAlertsDtoPage(request, accidentAlertsDto));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 AccidentAlerts
     * @author zhangwei
     * @date 2024-08-22 09:19:50
     */
    @Log("新增AccidentAlerts")
    @PostMapping("accidentAlerts")
    @ResponseBody
    @RequiresPermissions("accidentAlerts:add")
    public FebsResponse addAccidentAlerts(@RequestBody AccidentAlertsDto accidentAlertsDto) {
        try {
            //必填校验
            checkNecessaryColumns(accidentAlertsDto,false);
            AccidentAlerts accidentAlerts = BeanHelper.copyProperties(accidentAlertsDto,AccidentAlerts.class);
            accidentAlerts.setCreatedBy(getCurrentUser().getUserName());
            this.accidentAlertsService.createAccidentAlerts(accidentAlerts,accidentAlertsDto.getAccidentAlertsFileList());
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增AccidentAlerts失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
     * 逻辑删除 AccidentAlerts
     * @author zhangwei
     * @date 2024-08-22 09:19:50
     */
    @Log("逻辑删除AccidentAlerts")
    @GetMapping("accidentAlerts/delete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentAlerts:delete")
    public FebsResponse deleteAccidentAlerts(@PathVariable("id") String accidentAlertsIds) {
        try {
            AccidentAlerts accidentAlerts = new AccidentAlerts();
            accidentAlerts.setDeletedBy(getCurrentUser().getUserName());
            accidentAlerts.setDeletedTime(new Date());
            accidentAlerts.setDeletedFlg(FebsConstant.INVALID);
            this.accidentAlertsService.deleteAccidentAlerts(accidentAlerts,accidentAlertsIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentAlerts失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
    * 物理删除 AccidentAlerts
    * @author zhangwei
    * @date 2024-08-22 09:19:50
    */
    @Log("物理删除AccidentAlerts")
    @GetMapping("accidentAlerts/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentAlerts:delete")
    public FebsResponse physicallyDeleteAccidentAlerts(@PathVariable("id") String accidentAlertsIds) {
        try {
            this.accidentAlertsService.physicallyDeleteAccidentAlerts(accidentAlertsIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentAlerts失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
     * 修改 AccidentAlerts
     * @author zhangwei
     * @date 2024-08-22 09:19:50
     */
    @Log("修改AccidentAlerts")
    @PostMapping("accidentAlerts/update")
    @ResponseBody
    @RequiresPermissions("accidentAlerts:update")
    public FebsResponse updateAccidentAlerts(@RequestBody AccidentAlertsDto accidentAlertsDto) {
        //必填校验
        checkNecessaryColumns(accidentAlertsDto,true);
        try {
            AccidentAlerts accidentAlerts = BeanHelper.copyProperties(accidentAlertsDto,AccidentAlerts.class);
            accidentAlerts.setUpdatedBy(getCurrentUser().getUserName());
            this.accidentAlertsService.updateAccidentAlerts(accidentAlerts,accidentAlertsDto.getAccidentAlertsFileList());
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改AccidentAlerts失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
     * 导出 AccidentAlerts的excel
     * @author zhangwei
     * @date 2024-08-22 09:19:50
     */
    @PostMapping("accidentAlerts/excel")
    @ResponseBody
    @RequiresPermissions("accidentAlerts:view")
    public void export(QueryRequest queryRequest, AccidentAlertsDto accidentAlertsDto, HttpServletResponse response) throws FebsException {
        try {
            List<AccidentAlerts> accidentAlertss = this.accidentAlertsService.findAccidentAlertsPage(queryRequest,accidentAlertsDto).getRecords();
            ExcelKit.$Export(AccidentAlerts.class, response).downXlsx(accidentAlertss, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改事项启用状态
     * @param accidentAlerts
     * @return
     * @throws FebsException
     */
    @Log("修改状态")
    @PostMapping("accidentAlerts/updateStatus")
    @ResponseBody
    @RequiresPermissions(value={"accidentAlerts:update"},logical= Logical.OR)
    public FebsResponse updateStatus(AccidentAlerts accidentAlerts) {
        try {
            accidentAlerts.setUpdatedBy(getCurrentUser().getUserName());
            accidentAlerts.setUpdatedTime(new Date());
            this.accidentAlertsService.updateById(accidentAlerts);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改状态失败";
            log.error(message, e);
            return failure(message);
        }
    }

    /**
     * 新增保存时，字段必填项校验
     * @author zhangwei
     * @date 2024-08-27 11:06:50
     */
    private FebsResponse checkNecessaryColumns(AccidentAlertsDto accidentAlertsDto,boolean editFlg) {
        if(editFlg && StringUtils.isBlank(accidentAlertsDto.getAlertsId())){
            return failure("主键不能为空");
        } else if(StringUtils.isBlank(accidentAlertsDto.getAlertsTitle())){
            return failure("警报主题不能为空");
        } else if(null == accidentAlertsDto.getPublishDate()){
            return failure("请填写发布日期不能为空");
        } else if(null == accidentAlertsDto.getDeadlineDate()){
            return failure("截至日期不能为空");
        } else if(StringUtils.isBlank(accidentAlertsDto.getAlertsType())){
            return failure("警报类型不能为空");
        } else if(StringUtils.isBlank(accidentAlertsDto.getAlertsContent())){
            return failure("警报内容不能为空");
        } else {
            return success();
        }
    }

}
